#include "stdafx.h"
#include "CViewImageControl.h"
#include "CLogFile.h"
#include <stdio.h>
#include "common_def.h"

//++ Add HoangNT5 2013/06/16
int CViewImageControl::m_nTotalImages = 0;
int CViewImageControl::m_nImagesPerPage = NUMBER_IMAGES_PER_PAGE;
int CViewImageControl::m_nNumberPages = 0;
int CViewImageControl::m_nCurrentPageIndex = 0;
//-- Add HoangNT5 2013/06/16

IMPLEMENT_DYNAMIC(CViewImageControl, CWnd)

BEGIN_MESSAGE_MAP(CViewImageControl, CWnd)
	ON_WM_MOUSEMOVE()

//	ON_WM_LBUTTONDOWN()
ON_WM_PAINT()
END_MESSAGE_MAP()

CString strFileType[]= {L"JPG",L"PNG",L"BMP"};


CViewImageControl::CViewImageControl(void)
{
	//m_lstImageControl = 0;
	m_lstImageControl = new CList<CImageControl*>;

	m_nIndexView = 0;
	m_nIndexCurrent = -1;  // no selected image current
	//	CList <CImageControl*> m_lstImageControl; 
	

}
CViewImageControl::~CViewImageControl(void)
{
	// Make sure that there is no cell element in board game
	while (m_lstImageControl->GetCount()>0) 
    {
		// Get one cell in board game
        CImageControl* temp = m_lstImageControl->RemoveTail();
		// delete
        delete temp;
        OutputDebugString(_T("Free List Element\n"));
    }
	//if m_lstImageControl
}
void CViewImageControl::SetNumberOfImage( int numberOfImage )
{
	m_nNumOfImage		= numberOfImage;
}
//CString strFileType[]= {L"JPG",L"PNG",L"BMP"};

void CViewImageControl::FindImageInPath( CString PathFolderImage )
{
	CString pathTypeImage = L"";
	WIN32_FIND_DATA fdJPG;  
	HANDLE handleJPG;
	FILE * m_pLogFile;
	fopen_s (&m_pLogFile, DEF_LOG_FILE_NAME,"a+");
	for ( int i = 0; i < 3; i++)
	{
		pathTypeImage.Format(L"%s\\*.%s",PathFolderImage,strFileType[i]);

		handleJPG =:: FindFirstFile (pathTypeImage ,&fdJPG);
		if(handleJPG!= INVALID_HANDLE_VALUE)
		{
			do {  

			//	CLogFile logfile;
				if ( fdJPG.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY )  
					continue;  
				TRACE ("%s\n", fdJPG.cFileName);  
			//	std::wstring ws(fdJPG.cFileName);
				//CString FileName(ws.c_str());
				CString FileName(fdJPG.cFileName);
				CString PathFolder_FileName; 
				PathFolder_FileName = L"";

				PathFolder_FileName.Format(L"%s\\%s",PathFolderImage,FileName);
				m_plstImageName.AddHead( PathFolder_FileName );
			} 
			while (:: FindNextFile (handleJPG, &fdJPG)); 

		}
	}
	if( m_pLogFile != 0 )
		fclose (m_pLogFile);
	:: FindClose (handleJPG);
}

void CViewImageControl::SetSizeOfImageControl( int Height, int Width )
{
	m_nWidthControl		= Width;
	m_nHeightControl	= Height;
}
int CViewImageControl::GetNumOfImage()
{
	return this->m_nNumOfImage;
}
int CViewImageControl::GetViewImageControlWidth()
{
	return this->m_nWidthControl;
}
int CViewImageControl::GetViewImageControlHeight()
{
	return this->m_nHeightControl;
}

void CViewImageControl::LoadViewImage( CString pathFolderImage )
{
	m_strPathFolderImage = pathFolderImage;
	FindImageInPath( pathFolderImage );
	DrawImage();
}

void CViewImageControl::DrawImage()
{
	CRect rect;

	rect.left  = DEF_INITIAL_LEFT_IMAGE_CONTROL + BUTTON_TOP_VIEW_WIDTH;
	rect.right = DEF_WIDTH_IMAGE_CONTROL + BUTTON_TOP_VIEW_WIDTH;
	rect.top   = DEF_INITIAL_TOP_IMAGE_CONTROL;
	rect.bottom = DEF_HIGHT_IMAGE_CONTROL;

	//for(int i = 0 ; i< m_plstImageName.GetCount() ; i++)
	//CreateImageFromFile
	//	for ( auto ImageName = m_plstImageName.b
	//{// Get first cell position

	//++ Add HoangNT5 2013/05/16 
    m_nTotalImages = m_plstImageName.GetCount();
	m_nNumberPages = m_nTotalImages/m_nImagesPerPage;
	m_nCurrentPageIndex = 0;
	//-- Add HoangNT5 2013/05/16

	POSITION pos = m_plstImageName.GetHeadPosition();
    int nIndex  = 0;
    int nWidth  = 0;
    int nHeight = 0;
    int nWidthParent  = rect.right - rect.left - BORDER_TOTAL_WIDTH_IMAGE_CONTROL; // substract for 2 because control has border
    int nHeightParent = rect.bottom - rect.top - BORDER_TOTAL_WIDTH_IMAGE_CONTROL;

	/* get next cell */
	CString cur = (CString)m_plstImageName.GetNext( pos );
	CImageControl *pTem = new CImageControl;
    pTem->SetIndex(nIndex);
    nIndex++;
    this->Calculate(cur, nWidthParent, nHeightParent, nWidth, nHeight);

	m_lstImageControl->AddHead( pTem );

		pTem->Create(	_T(""),
						WS_CHILD | WS_VISIBLE | BS_ICON | SS_NOTIFY,
						rect,
						this,
						1);
		
        pTem->SetImageHeight(nHeight);
		pTem->SetImageWidth(nWidth);
		pTem->CreateImageFromFile( cur );
	
	while(pos)
	{		
        rect.left += DEF_WIDTH_IMAGE_CONTROL;
        rect.top = 5 ;
        rect.right += DEF_WIDTH_IMAGE_CONTROL;
        rect.bottom = DEF_HIGHT_IMAGE_CONTROL;

		/* get next cell */
		CString cur = (CString)m_plstImageName.GetNext( pos );
		CImageControl *pTem = new CImageControl;
        pTem->SetIndex(nIndex);
        nIndex++;
        this->Calculate(cur, nWidthParent, nHeightParent, nWidth, nHeight);

		m_lstImageControl->AddHead( pTem );
		
		pTem->Create(	_T(""),
						WS_CHILD | WS_VISIBLE | BS_ICON | SS_NOTIFY,
						rect,
						this,
						1);
		//rc.right-rc.left), (INT)(rc.bottom-rc.top));
		//pTem->SetImageHeight(rect.bottom - rect.top );
		//pTem->SetImageWidth(rect.right - rect.left );

        pTem->SetImageHeight(nHeight);
		pTem->SetImageWidth(nWidth);

		pTem->CreateImageFromFile(cur);
	}
}

void CViewImageControl::Calculate(CString strImageName, int nWidthParent, int nHeightParent, int &nWidth, int &nHeight)
{
    Bitmap bitmap(strImageName);
    int nWidthImage  = bitmap.GetWidth();
    int nHeightImage = bitmap.GetHeight();

    float nWRatio = (float)nWidthImage /  nWidthParent;
    float nHRatio =  (float)nHeightImage /  nHeightParent;

    if (nWRatio == nHRatio)
    {
        nWidth  = nWidthParent;
        nHeight = nHeightParent;
    }
    else if (nWRatio > nHRatio && nWRatio >= 1)
    {
        nWidth  = nWidthParent;
        nHeight = nHeightImage  / nWRatio;
    }
    else if (nWRatio < nHRatio && nHRatio >= 1)
    {
        nWidth  = nWidthImage / nHRatio;
        nHeight = nHeightParent;
    }
    else
    {
        nWidth  = nWidthImage ; // / nHRatio
        nHeight = nHeightImage;
    }
}

// Event handler

//void CViewImageControl::OnMouseMove(UINT nFlags, CPoint point)
//{
//	//BoradCellImage->ScrollWindow(-50,0);
//	ScrollWindow( -50, 0 );
//}

//void CViewImageControl::SetIndexImage(int indexImage)
//{
//    this->m_nIndexImage = indexImage;
//
//    // TamNV2 - Add
//    POSITION pos = this->m_lstImageControl->GetHeadPosition();
//    for (int nIndex = 0; nIndex < this->m_lstImageControl->GetCount(); nIndex++)
//    {
//        CImageControl* pImage = this->m_lstImageControl->GetNext(pos);
//        if (pImage->GetIndex() != indexImage)
//        {
//            pImage->SetSelectedValue(false);
//        }
//        else
//        {
//            pImage->SetSelectedValue(true);
//        }
//    }
//}
//
//int CViewImageControl::GetIndexImage()
//{
//    return this->m_nIndexImage;
//}

//void CViewImageControl::RefeshMoveHover()
//{
//    // TamNV2 - Add
//    POSITION pos = this->m_lstImageControl->GetHeadPosition();
//    for (int nIndex = 0; nIndex < this->m_lstImageControl->GetCount(); nIndex++)
//    {
//        CImageControl* pImage = this->m_lstImageControl->GetNext(pos);
//        pImage->SetHoverValue(false);
//    }
//}

void CViewImageControl::RefeshOldImageControl( int index )
{
	if ( index < 0 ) return;
	
	if ( index > m_lstImageControl->GetCount()) return;
	 // TamNV2 - Add
	CImageControl* pImage = NULL;
	POSITION pos = this->m_lstImageControl->GetHeadPosition();
    for (int nIndex = 0; nIndex < this->m_lstImageControl->GetCount(); nIndex++)
    {
		
        pImage = this->m_lstImageControl->GetNext(pos);
		if (pImage->GetIndex() == index) break;
		//if ( nIndex == index ) break;
      //  pImage->SetHoverValue(false);
    }
	if (pImage!=NULL)
	{
		pImage->SetSelectedValue( false );
		pImage->SetHoverValue( false );
		pImage->Invalidate(1);
	}
	//return pImage;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Function: DoPaging()
//
// Description:	Show number of image per page  		
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	History:
//		2013.06.16	Nguyen Thanh Hoang		v01		Create New
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
void CViewImageControl::DoPaing()
{
		int nStartIndex = m_nCurrentPageIndex;
		int nEndIndex = nStartIndex + m_nImagesPerPage;

		POSITION sCurPosition = m_plstImageName.FindIndex(nStartIndex);
		CString cur = _T("");

		CRect rect;

		rect.left  = DEF_INITIAL_LEFT_IMAGE_CONTROL + BUTTON_TOP_VIEW_WIDTH;
		rect.right = DEF_WIDTH_IMAGE_CONTROL + BUTTON_TOP_VIEW_WIDTH;
		rect.top   = DEF_INITIAL_TOP_IMAGE_CONTROL;
		rect.bottom = DEF_HIGHT_IMAGE_CONTROL;

		int nIndex  = 0;
		int nWidth  = 0;
		int nHeight = 0;
		int nWidthParent  = rect.right - rect.left - BORDER_TOTAL_WIDTH_IMAGE_CONTROL; // substract for 2 because control has border
		int nHeightParent = rect.bottom - rect.top - BORDER_TOTAL_WIDTH_IMAGE_CONTROL;

		for(int i = nStartIndex; i < nEndIndex; i++)
		{
			CImageControl *pImageControlTemp = new CImageControl();

			rect.left += DEF_WIDTH_IMAGE_CONTROL;
	        rect.top = 5 ;
		    rect.right += DEF_WIDTH_IMAGE_CONTROL;
			rect.bottom = DEF_HIGHT_IMAGE_CONTROL;

			this->Calculate(cur, nWidthParent, nHeightParent, nWidth, nHeight);

			cur = m_plstImageName.GetNext(sCurPosition);

			pImageControlTemp->SetImageHeight(nHeight);
			pImageControlTemp->SetImageWidth(nWidth);
			pImageControlTemp->CreateImageFromFile(cur);
			pImageControlTemp->Create(	_T(""),
						WS_CHILD | WS_VISIBLE | BS_ICON | SS_NOTIFY,
						rect,
						this,
						1);
		
			delete pImageControlTemp;
		}
}

// ++Add HoangNT5 2013/06/23: Update number ImageItem on ViewImageControl
void CViewImageControl::OnPaint()
{
		CPaintDC dc(this); // device context for painting		
		CRect rectImageControl;
		CRect rectViewImageControl;
		CString strImageNameCur = _T("");

		int nImageIndex				= 0;
		int nWidthImage				= 0;
		int nHeightImage			= 0;
		int nWidthImageItem			= 0;
		int nHeightImageItem		= 0;
		int nNumberImageControls	= 0;
		
		POSITION pos = {NULL};

		// Set retangle for first ImageControl
		rectImageControl.left  = DEF_INITIAL_LEFT_IMAGE_CONTROL + BUTTON_TOP_VIEW_WIDTH;
		rectImageControl.right = DEF_WIDTH_IMAGE_CONTROL + BUTTON_TOP_VIEW_WIDTH;
		rectImageControl.top   = DEF_INITIAL_TOP_IMAGE_CONTROL;
		rectImageControl.bottom = DEF_HIGHT_IMAGE_CONTROL;

		// Set Width and Height of ImageItem
		nWidthImageItem  = rectImageControl.right - rectImageControl.left - BORDER_TOTAL_WIDTH_IMAGE_CONTROL;
		nHeightImageItem = rectImageControl.bottom - rectImageControl.top - BORDER_TOTAL_WIDTH_IMAGE_CONTROL;

		// Get first position of name of image in list
		pos = m_plstImageName.GetHeadPosition();
		
		// Get retangle of ViewImageControl
		GetParent()->GetClientRect(rectViewImageControl);

		// Update number ImageControl displayed on ViewImageControl
		nNumberImageControls = (rectViewImageControl.Width() - 2*BUTTON_TOP_VIEW_WIDTH - DEF_INITIAL_LEFT_IMAGE_CONTROL)/(DEF_WIDTH_IMAGE_CONTROL + DEF_INITIAL_LEFT_IMAGE_CONTROL);

		// Remove all ImageItem on ViewImageControl
		for(int i = 0; i < m_lstImageControl->GetCount(); i++ )
		{
			m_lstImageControl->RemoveHead()->DestroyWindow();
		}

		for(int i = 0; i < nNumberImageControls; i++)
		{
			strImageNameCur = m_plstImageName.GetNext( pos );

			//Create ImageControl
			CImageControl *pTem = new CImageControl();
			pTem->Create(	_T(""),
							WS_CHILD | WS_VISIBLE | BS_ICON | SS_NOTIFY,
							rectImageControl,
							this,
							1);
			
			//Calculate width and height of Image on Image control
			this->Calculate(strImageNameCur, nWidthImageItem, nHeightImageItem, nWidthImage, nHeightImage);

			//Draw Image on ImageControl
			pTem->SetImageHeight(nHeightImage);
			pTem->SetImageWidth(nWidthImage);
			pTem->CreateImageFromFile(strImageNameCur);
			pTem->SetIndex(nImageIndex);			

			//Add new ImageControl to list
			m_lstImageControl->AddHead( pTem );

			//Move to new position to draw new ImageControl
			rectImageControl.left += DEF_WIDTH_IMAGE_CONTROL;
			rectImageControl.right += DEF_WIDTH_IMAGE_CONTROL;

			//Increase ImageIndex for new ImageControl
			nImageIndex++;
		}
}
// --Add HoangNT5 2013/06/23: Update number ImageItem on ViewImageControl